from pynput.mouse import Listener
import time

# 记录按下的时间
mouse_down_time = None

# 按下事件处理
def on_click(x, y, button, pressed):
    global mouse_down_time
    if pressed:
        # 记录鼠标按下的时间
        mouse_down_time = time.time()
    else:
        # 计算按下的时长
        if mouse_down_time is not None:
            hold_duration = (time.time() - mouse_down_time) * 1000  # 转换为毫秒
            print(f"Mouse button held down for: {hold_duration:.2f} ms")
            mouse_down_time = None

# 启动监听器
with Listener(on_click=on_click) as listener:
    listener.join()
